package net.kazed.nextaction.task.context;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.kazed.nextaction.database.TaskContext;
import android.os.Parcel;
import android.os.Parcelable;

public class TaskContextList implements Parcelable, Iterable<TaskContext> {
   
   private static final long NULL_ID = -1;
   
   private List<TaskContext> taskContexts;

   public TaskContextList() {
      this.taskContexts = new ArrayList<TaskContext>();
   }
   
   public TaskContextList(List<TaskContext> taskContexts) {
      this.taskContexts = taskContexts;
   }
   
   private TaskContextList(Parcel in) {
      int size = in.readInt();
      taskContexts = new ArrayList<TaskContext>(size);
      for (int i=0; i<size; i++) {
         final long id = in.readLong();
         final int taskId = in.readInt();
         final int contextId = in.readInt();
         TaskContext taskContext = null;
         if (id == NULL_ID) {
            taskContext = new TaskContext(taskId, contextId);
         } else {
            taskContext = new TaskContext(id, taskId, contextId);
         }
         taskContexts.add(taskContext);
      }
   }

   public void setTaskContexts(List<TaskContext> taskContexts) {
      this.taskContexts = taskContexts;
   }

   public List<TaskContext> getTaskContexts() {
      return taskContexts;
   }
   
   public void add(TaskContext taskContext) {
      taskContexts.add(taskContext);
   }

   public void addAll(List<TaskContext> taskContexts) {
      this.taskContexts = taskContexts;
   }
   
   @Override
   public int describeContents() {
      return 0;
   }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
      dest.writeInt(taskContexts.size());
      for (TaskContext taskContext : taskContexts) {
         if (taskContext.getId() != null) {
            dest.writeLong(taskContext.getId());
         } else {
            dest.writeLong(NULL_ID);
         }
         dest.writeInt(taskContext.getTaskId());
         dest.writeInt(taskContext.getContextId());
      }
   }
   
   public static final Parcelable.Creator<TaskContextList> CREATOR = new Parcelable.Creator<TaskContextList>() {
      public TaskContextList createFromParcel(Parcel in) {
         return new TaskContextList(in);
      }
      
      public TaskContextList[] newArray(int size) {
         return new TaskContextList[size];
      }
   };

   @Override
   public Iterator<TaskContext> iterator() {
      return taskContexts.iterator();
   }

   public TaskContext get(int index) {
      return taskContexts.get(index);
   }

   public void remove(TaskContext taskContext) {
      taskContexts.remove(taskContext);
   }

   public void clear() {
      taskContexts.clear();
   }

   public boolean isEmpty() {
      return taskContexts.isEmpty();
   }

   public boolean containsContext(final int contextId) {
      boolean contains = false;
      for (Iterator<TaskContext> iterator = taskContexts.iterator(); !contains && iterator.hasNext();) {
         TaskContext taskContext = (TaskContext) iterator.next();
         if (taskContext.getContextId() == contextId) {
            contains = true;
         }
      }
      return contains;
   }

}
